﻿/*
 Plugin for smalljs toolkit
 Copyright (c) 2013 Walter M. Soto Reyes
 http://www.smalljs.com or https://smalljs.codeplex.com
 License: https://smalljs.codeplex.com/license
*/
(function ($) {
    smalljs.plugin({

        maxChars: function (max, callback) {
            ///<summary>
            /// Limit maximun characters on a text field
            ///</summary>
            ///<param name="max" type="Number">
            /// Maximun number of characters permitted
            ///</param>
            ///<param name="callback" type="function">
            ///Call back function that will received a parameter with the number of characters left (Ex. function(r){  })
            ///</param>
            ///	<returns type="this" />
            this.forEach(function () {
                (function (id) {
                    $(id).on('keyup', (function () {
                        var c = $(id).get[0].value.length;
                        if (c >= max) {
                            $(id).get[0].value = $(id).get[0].value.substring(0, max);
                        }
                        if (typeof (callback) === 'function') {
                            callback((max - c > -1) ? max - c : 0);
                        }
                    }));
                })(this);
            });
             
            return this;
        }
    });

})(sj);